package com.web.common.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author liuyanlong
 * @Description
 * @Date 2021/11/12 9:45
 **/
public class ChangeChar{
    public static final char UNDERLINE = '_';

    //驼峰转下划线
    public static String camelToUnderline(String param, Integer charType) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
            }
            if (charType == 2) {
                //统一都转大写
                sb.append(Character.toUpperCase(c));
            } else {
                //统一都转小写
                sb.append(Character.toLowerCase(c));
            }


        }
        return sb.toString();
    }

    //下划线转驼峰
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        // "_" 后转大写标志,默认字符前面没有"_"
        Boolean flag = false;
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                flag = true;
                continue;   //标志设置为true,跳过
            } else {
                if (flag == true) {
                    //表示当前字符前面是"_" ,当前字符转大写
                    sb.append(Character.toUpperCase(param.charAt(i)));
                    //重置标识
                    flag = false;
                } else {
                    sb.append(Character.toLowerCase(param.charAt(i)));
                }
            }
        }
        return sb.toString();
    }

    /**
     * @Author liuyanlong
     * @Description 对象列表 下划线转驼峰
     * @Date 2021/11/12 9:57
     **/
    public static List<Map<String, Object>> underlineToCamel(List<Map<String, Object>> paramMaps) {
        List<Map<String, Object>> paramMapNew = new ArrayList<>(paramMaps.size());
        paramMaps.forEach(param -> {
            Map<String, Object> paramNew = new HashMap<>();
            for (String key : param.keySet()) {
                final String keyNew = underlineToCamel(key);
                final Object value = param.get(key);
                paramNew.put(keyNew, value);
            }
            paramMapNew.add(paramNew);
        });
        return paramMapNew;
    }

}
